"use strict";
/* Controllers */
app.controller("adtypeDefineListController", ['$scope', '$compile', '$location', '$rootScope', '$routeParams', 'adtypeDefineListService', function ($scope, $compile, $location, $rootScope, $routeParams, adtypeDefineListService) {

    var vm = $scope.vm;

    vm.p = {};
    vm.result = {};

    var errorFn = function (result) {
        alert(result);
    };

    $scope.listAdtypeDefine = function () {
        var vm = $scope.vm;
        var params = {};
        var procFn = function (response) {
            var data = response.data;
            if (data == null) {
                return;
            }
            vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
        };
        adtypeDefineListService.listAdtypeDefine(params, procFn, errorFn);
    };

    $scope.initializeController = function () {
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
            pageList: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });

        $resultTable.bootstrapTable('hideColumn', 'updateTime');
        $resultTable.bootstrapTable('hideColumn', 'createTime');
        $resultTable.bootstrapTable('hideColumn', 'id');
        $scope.listAdtypeDefine();
    };

    $scope.initializeController();


    $scope.confirmDel = function (id, index) {
        vm.toDelId = id
        vm.toDelIndex = index
        $('#myModal').modal('show')
    }

    $scope.updateValid = function (isValid) {
        var params = {};
        params.isValid = isValid
        var procFn = function (response) {
            if (response.success && response.data) {
                alert("更新成功")
            } else {
                alert("更新失败：" + response.msg);
            }
        };
        adtypeDefineListService.updateValid(params, procFn, errorFn);
    }

    $scope.ok = function () {
        var vm = $scope.vm;
        console.log("toDelId" + vm.toDelId)
        console.log("toDelIndex" + vm.toDelIndex)
        var params = {};
        params.id = vm.toDelId
        var procFn = function (response) {
            if (response.success && response.data) {
                $('#list_table').bootstrapTable('remove', {
                    field: 'id',
                    values: [parseInt(vm.toDelId)]
                });
                alert("删除成功！")
            } else {
                alert(response.msg);
            }
        };
        adtypeDefineListService.delAdtypeDefine(params, procFn, errorFn);
        $('#myModal').modal('hide')
    }

    $scope.setAdtypeValid = function (ths, id) {
        $(ths).attr("disabled", true);
        var btnText = $(ths).text()
        var params = {};
        params.id = id;
        if (btnText == '发布') {
            params.isValid = 1;
        } else {
            params.isValid = 0;
        }

        var procFn = function (response) {
            if (response.success && response.data) {
                if (btnText == '发布') {
                    $(ths).text('取消发布')
                    $(ths).removeClass('btn-success').addClass('btn-danger')
                } else {
                    $(ths).text('发布')
                    $(ths).removeClass('btn-danger').addClass('btn-success')
                }
                $(ths).attr("disabled", false);
            } else {
                alert(response.msg);
            }
        };
        adtypeDefineListService.updateValid(params, procFn, errorFn);
    }

}]);

function isLegoOpt(value, row, index) {
    if (value == 0) {
        return "否"
    } else {
        return "是"
    }
}

function isChargingOpt(value, row, index) {
    if (value == 0) {
        return "否"
    } else {
        return "是"
    }
}
function isValidOpt(value, row, index) {
    if (value == 0) {
        return "未发布"
    } else {
        return "已发布"
    }
}
function adtypeConfigOpt(value, row, index) {
    var btnText = '发布'
    var btnClass = 'success'

    if (row.isValid == 1) {
        btnText = '取消发布'
        btnClass = 'danger'
    }
    var optCont = '<div style="width:14rem"><a type="button" class="btn btn-xs btn-' + btnClass + '" onclick=angular.element(this).scope().setAdtypeValid(this,' + row.id + ') >' + btnText + '</a>\n'
    var optCont2 = '<a type="button" class="btn btn-xs btn-primary" href="#/star/config/adtype_define_edit/' + row.id + '" target="_blank">修改</a>\n'
    var optCont3 = '<a type="button" class="btn btn-xs btn-danger" onclick=angular.element(this).scope().confirmDel("' + row.id + '",' + index + ') >删除</a></div>'
    return [
        optCont,
        optCont2,
        optCont3
    ].join('');
}



/* Services */
app.service('adtypeDefineListService', ['ajaxService', function (ajaxService) {

    this.listAdtypeDefine = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/adDefine/list.do", successFunction, errorFunction);
    };

    this.delAdtypeDefine = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/adDefine/del.do", successFunction, errorFunction);
    };

    this.updateValid = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/adDefine/updateValid.do", successFunction, errorFunction);
    };

}]);
